import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '@/store/auth'

// 路由配置
const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: { title: '登录' }
  },
  {
    path: '/share/:code',
    name: 'Share',
    component: () => import('@/views/Share.vue'),
    meta: { title: '文件分享', requireAuth: false }
  },
  {
    path: '/',
    component: () => import('@/views/Layout.vue'),
    redirect: '/home',
    children: [
      {
        path: 'home',
        name: 'Home',
        component: () => import('@/views/Home.vue'),
        meta: { title: '首页' }
      },
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/Dashboard.vue'),
        meta: { title: '数据大屏', icon: 'DataLine' }
      },
      {
        path: 'files',
        name: 'Files',
        component: () => import('@/views/Files.vue'),
        meta: { title: '文件管理', icon: 'Folder' }
      },
      {
        path: 'users',
        name: 'Users',
        component: () => import('@/views/Users.vue'),
        meta: { title: '用户管理', icon: 'User' }
      },
      {
        path: 'transfer',
        name: 'Transfer',
        component: () => import('@/views/Transfer.vue'),
        meta: { title: '传输队列', icon: 'Connection' }
      },
      {
        path: 'remote-transfer',
        name: 'RemoteTransfer',
        component: () => import('@/views/RemoteTransfer.vue'),
        meta: { title: '远程传输', icon: 'Upload' }
      },
      {
        path: 'logs',
        name: 'Logs',
        component: () => import('@/views/Logs.vue'),
        meta: { title: '日志管理', icon: 'Document' }
      },
      {
        path: 'clients',
        name: 'Clients',
        component: () => import('@/views/Clients.vue'),
        meta: { title: '客户端监控', icon: 'Monitor' }
      },
      {
        path: 'software',
        name: 'Software',
        component: () => import('@/views/Software.vue'),
        meta: { title: '软件共享', icon: 'Box' }
      },
      {
        path: 'notifications',
        name: 'Notifications',
        component: () => import('@/views/Notifications.vue'),
        meta: { title: '通知管理', icon: 'Bell' }
      },
      {
        path: 'collab/projects',
        name: 'CollabProjects',
        component: () => import('@/views/copy1.vue'),
        meta: { title: '项目管理' }
      },
      {
        path: 'collab/creative',
        name: 'CollabCreative',
        component: () => import('@/views/copy2.vue'),
        meta: { title: '创意列表' }
      },
      {
        path: 'collab/software',
        name: 'SoftwareList',
        component: () => import('@/views/SoftwareList.vue'),
        meta: { title: '软件列表' }
      },
      {
        path: 'collab/software/create',
        name: 'SoftwareCreate',
        component: () => import('@/views/SoftwareCreate.vue'),
        meta: { title: '添加软件' }
      },
      {
        path: '404',
        name: 'NotFound',
        component: () => import('@/views/NotFound.vue'),
        meta: { title: '页面未找到' }
      }
    ]
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/404'
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const authStore = useAuthStore()

  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - 文件同步器` : '文件同步器'

  // 公开页面（分享页面）不需要登录
  if (to.meta.requireAuth === false) {
    next()
    return
  }

  // 检查登录状态
  if (to.path !== '/login' && !authStore.isLoggedIn) {
    next('/login')
  } else if (to.path === '/login' && authStore.isLoggedIn) {
    next('/')
  } else {
    next()
  }
})

export default router
